Download
FAQ
History
HomeHomeNext API
Search
Feedback
Divider

EJB-Tier Security

The following sections describe declarative and programmatic security mechanisms that can be used to protect resources in the EJB tier. The protected resources include methods of enterprise beans that are called from application clients, Web components, or other enterprise beans.

You can protect EJB-tier resources by doing the following:

For information about mapping roles to J2EE users and groups, see Mapping Roles to Users and Groups.

Declaring Method Permissions

After you've defined the roles (see Security Roles), you can define the method permissions of an enterprise bean. Method permissions indicate which roles are allowed to invoke which methods. You can define method permissions in different ways.

Using Programmatic Security in the EJB Tier

Programmatic security in the EJB tier consists of the getCallerPrincipal and the isCallerInRole methods. You can use the getCallerPrincipal method to determine the caller of the enterprise bean, and the isCallerInRole method to determine if the caller has the specified role.

The getCallerPrincipal method of the EJBContext interface returns the java.security.Principal object that identifies the caller of the enterprise bean. (In this case, a principal is the same as a user.) In the following example, the getUser method of an enterprise bean returns the name of the J2EE user that invoked it:

public String getUser() {
   return context.getCallerPrincipal().getName();
} 

You can determine whether an enterprise bean's caller belongs to the Customer role.

boolean result = context.isCallerInRole("Customer"); 

Unauthenticated User Name

Web applications accept unauthenticated Web clients and allow these clients to make calls to the EJB container. The EJB specification requires a security credential for accessing EJB methods. Typically, the credential will be that of a generic unauthenticated user.

Divider
Download
FAQ
History
HomeHomeNext API
Search
Feedback
Divider

All of the material in The J2EE 1.4 Tutorial is copyright-protected and may not be published in other works without express written permission from Sun Microsystems.